package com.yirui.bridge;

import com.yirui.bridge.core.Server;
import com.yirui.bridge.core.ServerManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@Slf4j
public class YrAgentBridgeApplication implements ApplicationRunner {

    @Autowired
    private ServerManager serverManager;


    public static void main(String[] args) {
        SpringApplication.run(YrAgentBridgeApplication.class, args);
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {


        if(args.containsOption("host") || args.containsOption("port")){
            int port = Integer.parseInt(args.getOptionValues("port").get(0));
            String host = args.containsOption("host")?args.getOptionValues("host").get(0):"localhost";


            String protocol = args.containsOption("protocol")? args.getOptionValues("protocol").get(0):"udp";
            Server server = serverManager.getServer(protocol, true);
            if(server == null){
                log.error("执行命令参数无效, 参数 [--protocol=tcp/udp/tcpServer default udp] [--host=主机] [--port=端口] [--c_timeout=连接超时设置]");
                System.exit(-1);
                return;
            }
            server.start(host, port, args);


        }else{

            log.error("执行命令参数无效, 参数 [--protocol=tcp/udp/tcpServer default udp] [--host=主机] [--port=端口] [--c_timeout=连接超时设置]");
            System.exit(-1);
        }

    }



}
